#!/usr/bin/python
# coding=utf8
#
# Copyright (C) 2011 XNData contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys, os
sys.path.append (os.getcwd () + '/../../')
import xndata.client
import xndata.client.photo
import unittest
import random

class XNPhotoClientTestCase (unittest.TestCase):

    def setUp (self):
        self.client = xndata.client.photo.XNPhotoClient (uname="zhangchitc@gmail.com", upass="aaaaaaaa")


    def testPassLogin (self):
        client = xndata.client.photo.XNPhotoClient (uname="zhangchitc@gmail.com", upass="aaaaaaaa")
        self.assertEqual (client.user_id, "242124580")
        self.assertEqual (client.user_name, "张弛")

    def testClientLogin (self):
        client = xndata.client.XNClient ("zhangchitc@gmail.com", "aaaaaaaa")
        pClient = xndata.client.photo.XNPhotoClient (xnclient=client)
        self.assertEqual (pClient.user_id, "242124580")
        self.assertEqual (pClient.user_name, "张弛")


    def test_get_total_album (self):
        # Ren Su Yi's album
        self.assertTrue (self.client.getTotalAlbum ("247780420") >= 12)
        # My album
        self.assertTrue (self.client.getTotalAlbum ("242124580") >= 41)


    def test_get_total_album_list (self):
        # Ren Su Yi's album
        self.assertTrue (self.client.getTotalAlbumList ("247780420") >= 1)
        # My album
        self.assertTrue (self.client.getTotalAlbumList ("242124580") >= 3)

    def test_get_next_album_list (self):
        # My albums
        album_list = self.client.getNextAlbumList (user_id="242124580")
        self.assertEqual (len (album_list.album_list), 16)
        album_list = self.client.getNextAlbumList (prev_album_list=album_list)
        self.assertEqual (len (album_list.album_list), 16)
        
        # Ren Su Yi's album
        album_list = self.client.getNextAlbumList (user_id="247780420")
        self.assertTrue (len (album_list.album_list) >= 12)
        album_list = self.client.getNextAlbumList (prev_album_list=album_list)
        self.assertTrue (len (album_list.album_list) >= 0)
        # print album_list

    def test_get_all_album (self):
        # My albums
        album_list = self.client.getAllAlbum (user_id="242124580")
        last = len (album_list) - 1
        self.assertEqual (album_list.owner_id, "242124580")
        lastalbum = album_list[last]
        self.assertEqual (lastalbum.owner_id, "242124580")
        self.assertEqual (lastalbum.album_id, "233079993")
        self.assertEqual (lastalbum.snapshot_url, 
        "http://fm411.img.xiaonei.com/pic001/20080321/17/03/main_868h58.jpg")
        self.assertEqual (lastalbum.description, "保送生旅游")
        self.assertEqual (lastalbum.total_photo, 17)

        # Ren Su Yi's album
        album_list = self.client.getAllAlbum (user_id="247780420")
        # print album_list

    def test_get_total_photo (self):
        self.assertEqual (self.client.getTotalPhoto ("242124580", "272296924"), 124)       

    def test_get_total_photo_list (self):
        self.assertEqual (self.client.getTotalPhotoList ("242124580", "272296924"), 7)       

    def test_get_next_photo_list (self):
        photo_list = self.client.getNextPhotoList (user_id="242124580", album_id="272296924") 
        self.assertEqual (photo_list.name, "垃圾桶")
        self.assertEqual (photo_list.site, "PKU")
        self.assertEqual (photo_list.description, "蛋疼")

        photo_profile = photo_list[0]
        self.assertEqual (photo_profile.owner_id, "242124580")
        self.assertEqual (photo_profile.photo_id, "4216364078")
        self.assertEqual (photo_profile.snapshot_url, 
        "http://fmn.xnpic.com/fmn051/20110417/0215/p_main_hSc8_734d00004ad45c42.jpg")
        self.assertEqual (photo_profile.description, "孙剑湿胸你成功出境了")

    def test_get_all_photo (self):
        photo_list = self.client.getAllPhoto (user_id="242124580", album_id="272296924") 
        last = len (photo_list) - 1
        self.assertEqual (last, 123)
        photo_profile = photo_list[last]

        self.assertEqual (photo_profile.owner_id, "242124580")
        self.assertEqual (photo_profile.photo_id, "1139885713")
        self.assertEqual (photo_profile.snapshot_url, 
        "http://fmo.xnimg.cn/fmn016/pic001/20081122/02/38/main_gGtf_4428e.jpg")
        self.assertEqual (photo_profile.description, "从笔袋深处扒出来的文物。。。哈哈我堕落的高三啊")

    def test_get_photo_detail (self):
        photo_detail = self.client.getPhotoDetail (user_id="247780420", photo_id="4136453711")
        self.assertEqual (photo_detail.owner_id, "247780420")
        self.assertEqual (photo_detail.photo_id, "4136453711")
        self.assertEqual (photo_detail.large_url, 
        "http://fmn.rrimg.com/fmn052/20110330/2325/p_large_TZh4_0c91000707df5c40.jpg")
        self.assertEqual (photo_detail.description, "@古运河")


suite1 = unittest.TestLoader().loadTestsFromTestCase (XNPhotoClientTestCase)
allsuite = unittest.TestSuite ([suite1])

unittest.TextTestRunner(verbosity=2).run(allsuite)

